( function ( D ) {

    D.get = function ( resourceId ) 
    {
        D.request( "graph_get.txt",
            {
                "resourceId": resourceId
            },
            function( results )
            {
                var i = 0,
                    p,
                    predicate,
                    predicateLabel,
                    id = resourceId,
                    oId;

                if( results.length > 0 ) 
                {
                    if( results[ 0 ].thumb ) 
                    {
                        if( !( D.elements[ id ] ) ) 
                        {
                            D.elements[ id ] = new D.Elem( D.r.image( results[0].thumb.value, 0, 0, 75, 
                                                                Math.floor( ( 75 * results[0].height.value ) / 
                                                                            results[0].width.value )), id );
                        }
                        i = 1;
                    }

                    for( ; i < results.length ; i += 1 ) 
                    {
                        p = results[ i ].p.value;
                        oId = results[ i ].o.value;

                        if( !( ( p === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ||
                               ( p === "http://desanti.org/schemas/collections#image" ) ||
                               ( p === "http://desanti.org/schemas/collections#thumbnail" ) ||
                               ( p === "http://desanti.org/schemas/collections#parentOf" ) ||
                               ( p === "http://desanti.org/schemas/collections#insertedAt" ) ||
                               ( p === "http://desanti.org/schemas/collections#childOf" ) ||
                               ( p === "http://desanti.org/schemas/collections#collection" ) ) ) 
                        {
                            if( results[i].oThumb ) 
                            {
                                if( !( D.elements[ oId ] ) ) 
                                {
                                    D.elements[ oId ] = new D.Elem( D.r.image( results[i].oThumb.value, 0, 0, 75,
                                                                         Math.floor( ( 75 * results[i].oHeight.value ) / 
                                                                                     results[i].oWidth.value )), oId );
                                    D.todo.push( oId );
                                }
                            } 
                            else 
                            {
                                if( results[ i ].oLabel ) 
                                {
                                    if( !( D.elements[ oId ] ) ) 
                                    {
                                        D.elements[ oId ] = new D.Elem( D.r.text( 0, 0, decodeURIComponent(results[i].oLabel.value) ).attr( {
                                                                        "stroke": "#aaa",
                                                                        "font-size": 20, 
                                                                        "fill": "#aaa"
                                                                    } ), oId);
                                        D.todo.push( oId );
                                    }
                                }
                            }

                            if( D.elements[ oId ] ) 
                            {
                                predicateLabel = p.match( /#(.*)$/ );
                                if( !( predicateLabel === null ) ) 
                                {
                                    D.elements[ id ].linkTo( [ D.elements[ oId ] ], decodeURIComponent(predicateLabel[1]) );
                                } 
                            }
                        }
                    }
                }

		D.request("graph_get_intervals.txt",
			  {
			      resourceId: resourceId
			  },
			  function(results){
			      var intervalId, relationLabel, intervalValue, start, end, text;

			      if(results.length > 0){ 
				  for(var i=0; i < results.length; i+=1){
				      intervalId = results[i].interval.value;
				      relationLabel = decodeURIComponent(results[i].relationLabel.value);
				      start = parseInt(results[i].start.value);
				      end = parseInt(results[i].end.value);
				      text = decodeURIComponent(results[i].text.value);
				      intervalValue = text.slice(start - 1, end);
				      if(!(D.elements[intervalId])){
					  D.elements[intervalId] = new D.Elem(D.r.text(0, 0, intervalValue).attr( {
					      "stroke": "#aaa",
					      "font-size": 20, 
					      "fill": "#aaa"
					  } ), intervalId);
				      }
				      D.elements[resourceId].linkTo([D.elements[intervalId]], relationLabel);
				  }
			      }		      
			  });
            });
    }
} ) ( DESANTI );
